//
//  YIem_SY_Three_ViewController.m
//  B段_开眼
//
//  Created by YIem on 16/5/6.
//  Copyright © 2016年 Howie. All rights reserved.
//

#import "YIem_SY_Three_ViewController.h"

@interface YIem_SY_Three_ViewController ()<UIWebViewDelegate, UIScrollViewDelegate>
@property (nonatomic, weak) UIWebView *web;

@end

@implementation YIem_SY_Three_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"YIem - 必应";
    // 返回
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_back_normal2"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:0.2077 green:0.2077 blue:0.2077 alpha:1.0];
    
    
    UIWebView *web = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    
    UIBarButtonItem *itemCollect = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_back_normal"] style:UIBarButtonItemStyleDone target:self action:@selector(leftAction1)] ;
    itemCollect.tintColor = [UIColor grayColor];
    UIBarButtonItem *itemShare = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_back_normal2"] style:UIBarButtonItemStyleDone target:self action:@selector(leftAction2)];
    itemShare.tintColor = [UIColor grayColor];
    self.navigationItem.leftBarButtonItems = @[itemCollect, itemShare];

    
    
    // 必应
    [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://cn.bing.com/?FORM=Z9FD1"]]];
    
    web.delegate = self;
    web.scalesPageToFit = YES;
    web.scrollView.delegate = self;
    self.web = web;
    [self.view addSubview:web];
    
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"开始加载");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"加载完成");
    // 获取当前页面的Title
    NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    NSLog(@"Title: %@", title);
//    self.navigationItem.title = title;
    // 获取当前URL
//    NSString *URL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
//    NSLog(@"URL: %@", URL);
    // 得到网页代码
//    NSString *html = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML" ];
//    NSLog(@"Html: %@", html);
    
    // 拼接字符串 根据网页Nmae
    NSString *str = @"YIem";
    NSString *jSStr = [NSString stringWithFormat:@"document.getElementsByName('q')[0].value = ('%@');",str];
    [webView stringByEvaluatingJavaScriptFromString:jSStr];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    
    NSLog(@"加载失败＝＝＝%@",error);
    
}
//当网页位置为顶部 不允许继续下拉
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (self.web.frame.origin.y == 0) {
        
        self.web.scrollView.bounces = NO;
        
        return;
    }
}
//webView的每次页面跳转都会执行，在这里可以得到想要的数据
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
//    NSLog(@"页面跳转");
    
    return YES;
    
}

//返回
- (void)leftAction1
{
    [self.web goBack];
}
- (void)leftAction2
{
     [self.web goForward];
}

- (void)rightAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
